package cn.bonoon.kernel.menus;

import java.util.HashMap;

/**
 * 生成菜单项，在编辑或查看菜单权限的时候，用于生成菜单项或菜单选项
 * @author jackson
 *
 */
public class MenuItemBuilder extends HashMap<String, Number>{
	
	private static final long serialVersionUID = 7003774966827428176L;
	
	private final MenuCheckable menuCheckable;
//	private final boolean button;
	private final boolean only;
	private final PlatformContext platformContext;
	
	public static MenuItemBuilder valueOf(PlatformContext platformContext, MenuCheckable menuCheckable, boolean only/*, boolean button*/){
		return new MenuItemBuilder(platformContext, menuCheckable, only/*, button*/);
	}
	
	MenuItemBuilder(PlatformContext platformContext, MenuCheckable menuCheckable, boolean only/*, boolean button*/){
		this.menuCheckable = menuCheckable;
//		this.button = button;
		this.platformContext = platformContext;
		this.only = only;
	}
	
	public String build(ItemableDefinition item){
		return internalBuild(item, ItemableDefinition.CHECKER_NAME, item.getId(), containsKey(item.getId()));
	}
	
	public String buildActions(MenuableDefinition item){
		//菜单本身有用的情况下
		ActionDefinition[] actionArray = item.getActions();
		if (null == actionArray || actionArray.length == 0) return "";
		
		StringBuilder menBuilder = new StringBuilder();
		Number actions = get(item.getId());
		
		menBuilder.append("&nbsp;&nbsp;权限：");
		for (ActionDefinition action: actionArray) {
			menBuilder.append(build(action, actions));
		}
		
		return menBuilder.toString();
	}
	
	public StringBuilder buildTabs(ChannelDefinition channel){
		StringBuilder tbsBuilder = new StringBuilder();
		for(TabsDefinition ti : channel.getTabs()){
			
			StringBuilder tabBuilder = new StringBuilder();
			for(TabItemDefinition item : ti){
				String tiBuilder = build(item);
				if(tiBuilder.isEmpty()) continue;
				tabBuilder.append(tiBuilder);
			}
			
			if(tabBuilder.length() > 0){
				tbsBuilder.append("<li>").append(ti.getName());
				buildButton(tbsBuilder, ti);
				tbsBuilder.append("&nbsp;&nbsp;Item：").append(tabBuilder).append("</li>");
			}
		}
		
		return tbsBuilder;
	}

	public void buildButton(StringBuilder html, final ItemableDefinition item) {
		if(checkable()){
			html.append("<a href='javascript:void(0);' name='privileges_allsel' class='").append(item.getXpath()) .append("' style='padding-left:40px;'>全选</a>");
			html.append("<span style='color:#bfbfbf;'>&nbsp;|&nbsp;</span>");
			html.append("<a href='javascript:void(0);' name='privileges_cancel' class='").append(item.getXpath()) .append("'>取消</a>");
		}
	}
	
	public String build(ActionDefinition item, Number actions){
		return internalBuild(item, item.getMenu().getId(), item.getKey(), item.match(actions));
	}
	
	private String internalBuild(ItemableDefinition item, String inputName, Object inputValue, boolean checked){
		if(only && !checked) return ""; // 只显示有权限的菜单，没有权限的，则直接跳过
		
		boolean needLabel = false;
//		PlatformContext platformContext = item.getPlatformContext();
		StringBuilder html = new StringBuilder();
		
		switch (menuCheckable) {
		case CHECKABLE:
			needLabel = true;
			html.append("<input name='").append(inputName);
			html.append("' class='").append(item.getXpath());
			html.append("' value='").append(inputValue);
			html.append("' id='").append(item.getId());
			if(checked){ html.append("' checked='checked"); }
			html.append("' type='checkbox'/>");

			break;
		case READONLY:
			html.append("<img src='").append(platformContext.iconCheckable(checked)).append("'/>");

			break;
		case UNCHECKED:
			needLabel = true;
			html.append("<input name='").append(inputName);
			html.append("' class='").append(item.getXpath());
			html.append("' value='").append(inputValue);
			html.append("' id='").append(item.getId());
			html.append("' type='checkbox'/>");

			break;
		}
		
		/*if(button){
			
		}else */
		if(needLabel){
			html.append("<label for='").append(item.getId()).append("'>");
			html.append(item.getName());
			html.append("</label>");
		}else{
			html.append(item.getName());
		}
		return html.toString();
	}
	
	public boolean checkable(){
		return menuCheckable.checkable();
	}
}
